package com.huli.controller;

import cn.hutool.core.lang.Assert;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.huli.domain.PropertyCompany;
import com.huli.dto.OrdersInfoDto;
import com.huli.dto.PayOrdersDto;
import com.huli.dto.PropertyCompanyDto;
import com.huli.dto.PropertyLoginDto;
import com.huli.feign.CarportFeign;
import com.huli.feign.OwnerFeign;
import com.huli.param.AppealParam;
import com.huli.param.CarportParam;
import com.huli.service.OrdersService;
import com.huli.service.PropertyCompanyService;
import com.huli.utils.JwtUtils;
import com.huli.utils.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "物业服务")
@RestController
@RequestMapping("propertyCompany")
public class PropertyCompanyController {
    @Autowired
    PropertyCompanyService propertyCompanyService;
    @Autowired
    CarportFeign carportFeign;
    @Autowired
    OwnerFeign ownerFeign;
    @Autowired
    OrdersService ordersService;


    @PostMapping("login")
    @ApiOperation("物业登陆接口")
    public Result propertyLogin(@RequestBody PropertyLoginDto propertyLoginDto){
        PropertyCompany property = propertyCompanyService.getOne(new QueryWrapper<PropertyCompany>()
                .eq("property_company_account", propertyLoginDto.getAccount()));
        Assert.notNull(property,"物业账号或密码错误");
        boolean b = property.getPropertyCompanyPassword().equals(propertyLoginDto.getPassword());
        Assert.isTrue(b,"物业账号或密码错误!");

        JwtUtils jwtUtils = new JwtUtils();

        return Result.successResult(jwtUtils.generateToken(property.getPropertyCompanyId()));
    }

    //查询车库所有的车位 调用6.5
    @ApiOperation("查询车库所有的车位")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "garageId", value = "车库id"),
            @ApiImplicitParam(name = "pageIndex", value = "页码"),
            @ApiImplicitParam(name = "pageNum", value = "显示个数")})
    @GetMapping("selectAllCarports")
    public Result selectAllCarports(CarportParam carportParam) {
        Result result = carportFeign.selectAllCarport(carportParam);
        return Result.successResult(result);
    }

    //查询车库所有已支付订单
    @ApiOperation("查询车库所有已支付订单")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "garageId", value = "车库id")
    })
    @GetMapping("selectAllPayOrders")
    public Result selectAllPayOrders(int garageId) {
        List<PayOrdersDto> payOrdersDtos = ordersService.selectAllPayOrders(garageId);
        return Result.successResult(payOrdersDtos);
    }


    //查询车库订单详情
    @ApiOperation("查询车库订单详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "garageId", value = "车库id")
    })
    @GetMapping("selectCarportOrdersInfo")
    public Result selectCarportOrdersInfo(int garageId) {
        List<OrdersInfoDto> ordersInfoDtos = propertyCompanyService.selectCarportOrdersInfo(garageId);
        return Result.successResult(ordersInfoDtos);
    }


    //生成申述表
    @ApiOperation("生成申述表")
    @PostMapping("createAppeal")
    public Result createAppeal(@RequestBody AppealParam appealParam) {
        propertyCompanyService.createAppeal(appealParam);
        return Result.successResult("生成成功!");
    }

    //查询所有物业的联系方式
    @ApiOperation("查询所有物业的联系方式")
    @GetMapping("selectAllPropertyPhone")
    public Result selectAllPropertyPhone() {
        List<PropertyCompanyDto> propertyCompanyDtos = propertyCompanyService.selectAllPropertyPhone();
        return Result.successResult(propertyCompanyDtos);
    }


}
